import 'dart:convert';
import 'dart:io';

class FileTodoUtil {
  /// 加载todoList列表到内存
  static Future<List> loadTodoList(File file) async {
    var fileContent = await file.readAsString();

    if (fileContent.isEmpty) {
      fileContent = '[]';
    }
    // 解析json加载到内存
    var jsonList = jsonDecode(fileContent);

    if (jsonList == null || !(jsonList is List)) {
      jsonList = [];
    }
    return jsonList;
  }

  /// 储存String到file
  static Future<File> saveStrToFile(File file, String todoListJsonStr) {
    return file.writeAsString(todoListJsonStr, flush: true);
  }
}
